package com.asset.salesforce.dao;



import java.util.ArrayList;
import java.util.List;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.StockSubCategory;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class StockSubCategoryDaoImpl extends BasicDaoImpl<StockSubCategory>{

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public StockSubCategory getBySubName(String name) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockSubCategory where name=  :name");
		 query.setParameter("name", name);
		 List<StockSubCategory>list  = new ArrayList<StockSubCategory>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
	
	}

	@SuppressWarnings("unchecked")
	public List<StockSubCategory> getAllSubCategoriesByCategory(Integer stockCategoryId,Integer employeeId) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockSubCategory where stockCategory.id = :stockCategoryId And owner.id=:employeeId");
		 query.setParameter("stockCategoryId", stockCategoryId);
		 query.setParameter("employeeId", employeeId);
		 List<StockSubCategory>list  = new ArrayList<StockSubCategory>();
		 list = query.list();
		 if(list.size() >0){
			 return list;
		 }else{
			 return new ArrayList<StockSubCategory>();
		 }
	
	}
	public List<StockSubCategory> getAllStockSubCategoriesByCompanyId(Employee employee){
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockSubCategory stockSubCategory where stockSubCategory.owner.company.id = :companyId");
		 query.setParameter("companyId", employee.getCompany().getId());
		 List<StockSubCategory>list  = new ArrayList<StockSubCategory>();
		 list = query.list();
			 return list;
	}
}
